package com.itboye.ihomebank.home;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.fangyuan.ZhiFuFangShiAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.HouseDetailBean;
import com.itboye.ihomebank.bean.HouseZiDianBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.HouseZiDianBean.housePay;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.me.ActivityFaBuHouseTwo;
import com.itboye.ihomebank.me.ActivityMyData;
import com.itboye.ihomebank.myzhujia.ActivityMySign;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.CameraPopupWindow;
import com.itboye.ihomebank.util.PayTypePopupWindow;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.TimesUtils;
import com.itboye.ihomebank.util.YuYueChooseTimePopwindow;
import com.itboye.ihomebank.util.YuYueChooseTimePopwindow.onFinishedLisenter;

/**
 * 
 */
public class ActivityShenQingZuFang extends BaseOtherActivity implements
		OnClickListener, Observer, onFinishedLisenter {
	Button btn_submit;
	EditText edit_content;
	TextView add_shap_title_tv;
	ImageView close_icon;
	HousePresenter housePresenter;
	private String houseNo;
	private String uid;
	private EditText edit_zujin;
	private String money;
	private TextView timestart;
	private String timeStart;
	private TextView timeend;
	private String timeEnd;
	private String fuKuan;
	private TextView fukuan;
	RelativeLayout fukuanfangshi, startTime, stopTime;
	ListView paytype;
	private PayTypePopupWindow payTypePopupWindow;
	private OnClickListener onClickListener;
	long currentTime;
	private long startT;
	private long endT;
	private RelativeLayout layout;
	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.btn_submit:
			money = edit_zujin.getText().toString();
			if (money.equals("")) {
				ByAlert.alert("请输入意向金额");
				return;
			}
			timeStart = timestart.getText().toString();
			if (timeStart.equals("")) {
				ByAlert.alert("请选择起租日期");
				return;
			}
			timeEnd = timeend.getText().toString();
			if (timeEnd.equals("")) {
				ByAlert.alert("请选择止租日期");
				return;
			}
			if (currentTime > startT) {
				ByAlert.alert("起租起始时间应该大于当前时间");
				return;
			}
			System.out.println(startT + ">?:" + endT);
			if (endT <= startT) {
				ByAlert.alert("止租时间应该大于起租时间");
				return;
			}
			fuKuan = fukuan.getText().toString();
			if (fuKuan.equals("")) {
				ByAlert.alert("请选择付款方式");
				return;
			}
			houseNo = getIntent().getStringExtra("house_no");
			uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
			// String remark = edit_content.getText().toString();
			// if (remark.equals("")) {
			// ByAlert.alert("请输入申请理由");
			// return;
			// }
			housePresenter.shenQingQianYue(uid, houseNo, "", "0", timeStart,
					timeEnd, money, fuKuan);
			// intent=new Intent(this,ActivityShenQingZuFang.class);
			// startActivity(intent);
			break;
		case R.id.fukuanfangshi:// 选择付款方式
			
			ZhiFuPopWindow(v);
			
//			payTypePopupWindow = new PayTypePopupWindow(this, onClickListener);
//			payTypePopupWindow.showAtLocation(v, Gravity.BOTTOM
//					| Gravity.CENTER_HORIZONTAL, 0, 0);
//			paytype = (ListView) payTypePopupWindow.getContentView()
//					.findViewById(R.id.paytype);
			// paytype.setAdapter(new )
			break;
		case R.id.stopTime:
			
			YuYueChooseTimePopwindow endTimePopwindow = new YuYueChooseTimePopwindow(
					this, this, false);
			endTimePopwindow.showAtLocation(v, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
			
			
			break;
		case R.id.startTime:
			YuYueChooseTimePopwindow startTimePopwindow = new YuYueChooseTimePopwindow(
					this, this, true);
			startTimePopwindow.showAtLocation(v, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
			break;
		default:
			break;
		}

	}
	private List<housePay> house_pay = new ArrayList<housePay>();// 支付方式
	private ZhiFuFangShiAdapter zhiFuFangShiAdapter;
	private PopupWindow mPopWin;
	protected String fangshi;
	/**
	 * 支付方式
	 */
	private void ZhiFuPopWindow(View v) {
		layout = (RelativeLayout) LayoutInflater
				.from(ActivityShenQingZuFang.this).inflate(
						R.layout.popup_fangyuan, null);

		ListView fangyuan_listView = (ListView) layout
				.findViewById(R.id.fangyuan_listView);

		if (house_pay != null) {
			if (house_pay.size() > 0) {

				zhiFuFangShiAdapter = new ZhiFuFangShiAdapter(this, house_pay);

				fangyuan_listView.setAdapter(zhiFuFangShiAdapter);

				mPopWin = new PopupWindow(layout,
						ViewGroup.LayoutParams.MATCH_PARENT,
						ViewGroup.LayoutParams.MATCH_PARENT, true);
				mPopWin.setBackgroundDrawable(new BitmapDrawable());
				mPopWin.showAsDropDown(v, 0, 0);
				mPopWin.update();

				fangyuan_listView
						.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> parent,
									View view, int position, long id) {
								fangshi = house_pay.get(position).getId();
								fukuan.setText(house_pay.get(position)
										.getName());
								mPopWin.dismiss();
							}
						});
			}
		}
	}


	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_reson;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		 currentTime=System.currentTimeMillis();
		add_shap_title_tv.setText("提交申请");
		housePresenter = new HousePresenter(this);
		housePresenter.houseZiDian("");
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.qianYueShenQing_success) {
				ByAlert.alert(entity.getData());
				finish();
			} else if (entity.getEventType() == HousePresenter.qianYueShenQing_fail) {
				ByAlert.alert(entity.getData());
			}else if (entity.getEventType() == HousePresenter.houseZiDian_success) {
				HouseZiDianBean houseZiDianBean = (HouseZiDianBean) entity
						.getData();
				house_pay.addAll(houseZiDianBean.getHouse_pay());
			}
		}
	}

	@Override
	public void getBeginTime(String year, String month, String day,
			String hour, String minite) {
		// TODO Auto-generated method stub
		startT = TimesUtils.getStringToDate(year + "年" + month + "月" + day
				+ "日 " + hour + ":" + minite);
		timestart.setText(year + "年" + month + "月" + day + "日 "
				+ hour + ":" + minite);
	}

	@Override
	public void getEndTime(String year, String month, String day, String hour,
			String minite) {
		// TODO Auto-generated method stub
		endT = TimesUtils.getStringToDate(year + "年" + month + "月" + day + "日 "
				+ hour + ":" + minite);
		timeend.setText(year + "年" + month + "月" + day + "日 " + hour
				+ ":" + minite);
	}

}
